/*===========================================

	MRS Viewer for Wii (Will be CG Mode?) 
	currently doesnt compile, dont know why, need to sleep now anyways...

	Coders:
		GizmoTheGreen
		TalkLittle

	Credits:
		NoNameNo

============================================*/

#include <ogcsys.h>
#include <wiiuse/wpad.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <fat.h>
#include <malloc.h>

#include "GRRLIB/GRRLIB.h"
#include "MRS.h"

Mtx GXmodelView2D;

// "Global" variables
//static char debugstr[1024];
int res;
int quit;
u32 type;
u32 wpaddown;
FILE *MRSfp;

int MRS_paletteNumber; // 0x0-0xF
u8 *MRS_bg_data = NULL;
u8 *MRS_sprite_data = NULL;
u32 MRS_bg_colorTable[256]; // 256 or 16??
u32 MRS_sprite_colorTable[256]; // 256 or 16??
short MRS_bg_width, MRS_bg_height;

#define FONTCOL 0xFF000000;
#define MRSfile "sd:/data/truelove/MRS"

// functions
void VBL(void); // waitvsync replacement :)

void Debug(const char * text);

void MRS_fillColorTable(const u8 *mrsPalette, u32 *colorTable, u8 alpha) {
	u8 i;
	u8 tmp[4];
	//Debug("MRS_fillColorTable\n");
	for (i = 0; i < 16; i++) {
		tmp[0] = mrsPalette[48*MRS_paletteNumber + 3*i + 1]; // R
		tmp[1] = mrsPalette[48*MRS_paletteNumber + 3*i + 2]; // G
		tmp[2] = mrsPalette[48*MRS_paletteNumber + 3*i + 0]; // B
		tmp[3] = alpha;                                      // A
		memcpy(&colorTable[i], tmp, 4);
	}
}

/**
 * Load MRS image into a buffer
 */
void MRS_ChangeFile(const char *mrsFile, u8 **outData, u32 *outColorTable,
		short *outWidth, short *outHeight) {
	MRSHEADER mrsheader;

	//Debug("MRS_ChangeFile: before MRS_extract_filename\n");
	*outData = MRS_extract_filename(MRSfp, mrsFile, outWidth, outHeight, mrsheader);
	//Debug("MRS_ChangeFile: before MRS_fillColorTable\n");
	MRS_fillColorTable(&mrsheader[MRS_PALETTE_OFFSET], outColorTable, 0xFF);
}

void MRS_Plot(u8 *data, u32 *colorTable, short xoff, short yoff,
		short width, short height) {
	u8 *cur;
	short x, y;

	//Debug("MRS_Plot: before x,y loop\n");
	cur = data;
	// Scanlines go from bottom to top
	for (y = 0; y < height; y++) {
		for (x = 0; x < width; x++) {
			GRRLIB_Plot(x + xoff, y + yoff, colorTable[*(cur++)]);
		}
	}
}

// MAIN!!!
int main()
{
	// These should only remain until main is over :) fade;
	//u32 fade;

	// General Video and Controller Inits
	GRRLIB_Init();
	WPAD_Init();
	// FAT Init
	fatInitDefault();

	MRSfp = fopen(MRSfile, "rb");

	MRS_paletteNumber = 0;
	int mrsfile = 0;
	int changed = 0;

	// Default to black screen
	MRS_ChangeFile("BLACK.MRS", &MRS_bg_data, MRS_bg_colorTable,
		&MRS_bg_width, &MRS_bg_height);
	
	while(!quit)
	{
		MRS_Plot(MRS_bg_data, MRS_bg_colorTable, 0, 40, MRS_bg_width, MRS_bg_height);

		if (wpaddown & (WPAD_BUTTON_PLUS | WPAD_BUTTON_RIGHT))
		{
			if (++mrsfile >= MRS_numFiles)
				mrsfile = 0;
			do {
				WPAD_ScanPads();
				wpaddown = WPAD_ButtonsDown(0);
			} while(wpaddown & (WPAD_BUTTON_PLUS | WPAD_BUTTON_RIGHT));
			changed = 1;
			MRS_paletteNumber = 0;
		}

		if (wpaddown & (WPAD_BUTTON_MINUS | WPAD_BUTTON_LEFT))
		{
			if (--mrsfile < 0)
				mrsfile = MRS_numFiles - 1;
			do {
				WPAD_ScanPads();
				wpaddown = WPAD_ButtonsDown(0);
			} while(wpaddown & (WPAD_BUTTON_MINUS | WPAD_BUTTON_LEFT));
			changed = 1;
			MRS_paletteNumber = 0;
		}

		if (wpaddown & (WPAD_BUTTON_UP))
		{
			if (++MRS_paletteNumber >= 16)
				MRS_paletteNumber = 0;
			do {
				WPAD_ScanPads();
				wpaddown = WPAD_ButtonsDown(0);
			} while(wpaddown & (WPAD_BUTTON_UP));
			changed = 1;
		}

		if (wpaddown & (WPAD_BUTTON_DOWN))
		{
			if (--MRS_paletteNumber < 0)
				MRS_paletteNumber = 15;
			do {
				WPAD_ScanPads();
				wpaddown = WPAD_ButtonsDown(0);
			} while(wpaddown & (WPAD_BUTTON_DOWN));
			changed = 1;
		}

		if (changed) {
			free(MRS_bg_data);
			MRS_ChangeFile(MRS_fileNames[mrsfile],
				&MRS_bg_data, MRS_bg_colorTable,
				&MRS_bg_width, &MRS_bg_height);
			changed = 0;
		}

		VBL();
	}

	// Clear allocated memory
	free(MRS_bg_data);
	fclose(MRSfp);
	GRRLIB_Exit();
	return 0;
}

void VBL() // waitvsync replacement :)
{
	// Wiimote stuff
	WPAD_ScanPads();
	// Detect buttons
	wpaddown = WPAD_ButtonsDown(0);
	// Quit
	if (wpaddown & WPAD_BUTTON_HOME)
	{
		quit = 1;
	}
	// Check wiimote stat
	GRRLIB_Render();
}

void Debug(const char * text)
{
	FILE * debug;
	debug = fopen("sd:/debug.txt", "a");
	fputs (text,debug);
	fclose(debug);
}

